<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sortNo" :min="1"></el-input-number>
			</el-form-item>
			<el-form-item label="人员账号" prop="accountId">
				<select-user-dialog v-model:accountId="form.accountId" :mode="false"/>
			</el-form-item>
			<el-form-item label="隐藏字段" prop="hideField">
				<el-checkbox-group v-model="hideField">
					<template v-for="item in formFieldList" :key="item.id">
						<el-checkbox :label="item.id" v-if="item.elType!=='data-fill'">{{ item.label }}</el-checkbox>
					</template>
				</el-checkbox-group>
			</el-form-item>
			<el-form-item label="是否生效">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注" prop="remark" style="margin-top:10px">
				<el-input v-model="form.remark" maxlength="150" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import selectUserDialog from "@/views/module/selectUserDialog.vue";

export default {
	components: {
		selectUserDialog
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			addTemplate: {
				formData: '',
				tags: ''
			},
			visible: false,
			isSaveing: false,
			hideField:[],
			formFieldList:[],
			//表单数据
			form: {
				recordId:'',
				sortNo: 1,
				status:"1",
				flowId:"",
				formId:"",
				accountId:"",
				hideField:"",
				remark: ""
			},
			//验证规则
			rules: {
				accountId: [
					{required: true, message: '人员账号不能为空', trigger: 'blur'}
				],
				hideField: [
					{required: true, message: '隐藏字段不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode='add'){
			this.mode = mode;
			this.visible = true;
			return this
		},
		async getBpmFormFieldsListForSelect() {
			var res = await this.$API.bpm.bpmForm.getBpmFormFieldsListForSelect.get({formId: this.form.formId});
			if (res.code == 200) {
				this.formFieldList = res.data;
			}
		},
		//表单提交方法
		submit(){
			this.form.hideField = this.hideField.join(",");
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.bpm.bpmReadFieldRole.setBpmReadFieldRole.post(this.form);
					this.isSaveing = false;
					if(res.code == 200){
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		createSetData(data){
			this.form.formId = data.formId
			this.form.flowId = data.flowId
			this.getBpmFormFieldsListForSelect();
		},
		setData(data){
			this.form.recordId = data.recordId
			this.form.sortNo = data.sortNo
			this.form.flowId = data.flowId
			this.form.formId = data.formId
			this.form.accountId = data.accountId
			this.form.hideField = data.hideField
			this.form.status = data.status
			this.form.remark = data.remark
			if(data.hideField!=undefined&&data.hideField!='')
			{
				this.hideField = data.hideField.split(",")
			}
			this.getBpmFormFieldsListForSelect();

		},

	}
}
</script>

<style>
</style>
